package com.example.wan_android.ui.activity

import android.text.Editable
import android.text.TextWatcher
import com.bumptech.glide.Glide
import com.example.wan_android.BR
import com.example.wan_android.R
import com.example.wan_android.databinding.ActivityRegisterBinding
import com.example.wan_android.viewmodel.RegisterViewModel

class RegisterActivity : BaseActivity<ActivityRegisterBinding, RegisterViewModel>(
    RegisterViewModel::class.java,
    R.layout.activity_register,
    BR.viewModel
) {
    override fun initView() {
        mViewModel?.context = this
        mViewModel?.showPassword?.observe(this) {
            if (it) {
                Glide.with(this).load(R.drawable.ic_un_display).into(mViewDataBinding?.ivDisplay!!)
            } else {
                Glide.with(this).load(R.drawable.display).into(mViewDataBinding?.ivDisplay!!)
            }
        }
        mViewModel?.againShowPassword?.observe(this) {
            if (it) {
                Glide.with(this).load(R.drawable.ic_un_display)
                    .into(mViewDataBinding?.ivDisplayAgain!!)
            } else {
                Glide.with(this).load(R.drawable.display).into(mViewDataBinding?.ivDisplayAgain!!)
            }
        }
        mViewModel?.currentPosition?.observe(this) {
            mViewDataBinding?.etPassword?.setSelection(it)
        }
        mViewModel?.againCurrentPosition?.observe(this) {
            mViewDataBinding?.etAgainPassword?.setSelection(it)
        }
        mViewDataBinding?.etPassword?.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun afterTextChanged(p0: Editable?) {
                mViewModel?.setCurrentPosition(p0?.length ?: 0)
            }
        })
        mViewDataBinding?.etAgainPassword?.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun afterTextChanged(p0: Editable?) {
                mViewModel?.setAgainCurrentPosition(p0?.length ?: 0)
            }
        })
        mViewDataBinding?.btRegister?.setOnClickListener({
            mViewModel?.register(
                mViewDataBinding?.etNumber?.text.toString() ?: "",
                mViewDataBinding?.etPassword?.text.toString() ?: "",
                mViewDataBinding?.etAgainPassword?.text.toString() ?: ""
            )
        })
    }
}